﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BITC.Library.Provider;
using VVA.Data.Entity;

namespace VVA.Data.Service
{
    public class ProductService
    {
        #region Declaration

        #endregion

        #region Method

        public Product GetByID(int id)
        {
            return SqlDataProvider.CreateInstance().ExecuteCollection<Product>("Proc_Product_SelectById", id).FirstOrDefault();
        }

        public Product GetBySlug(string slug)
        {
            return SqlDataProvider.CreateInstance().ExecuteCollection<Product>("Proc_Product_SelectBySlug", slug).FirstOrDefault();
        }

        public IList<Product> GetAll(string culture, int pageIndex, int pageSize, out int totalCount)
        {
            var _db = SqlDataProvider.CreateInstance();
            var cmd = _db.DB.GetStoredProcCommand("Proc_Product_SelectAll");
            _db.DB.DiscoverParameters(cmd);

            cmd.Parameters["@Culture"].Value = culture;
            cmd.Parameters["@PageIndex"].Value = pageIndex;
            cmd.Parameters["@PageSize"].Value = pageSize;
            var lst = _db.DataReaderToCollection<Product>(_db.DB.ExecuteReader(cmd));

            totalCount = (int)cmd.Parameters["@RETURN_VALUE"].Value;
            return lst;
        }

        public IList<Product> GetAllByTaxonomyID(Guid taxonomyID, string culture)
        {
            return SqlDataProvider.CreateInstance().ExecuteCollection<Product>("Proc_Product_SelectByTaxonomyId", taxonomyID, culture);
        }

        public IList<Product> GetAllByTaxonomySlug(string slug, string culture)
        {
            return SqlDataProvider.CreateInstance().ExecuteCollection<Product>("Proc_Product_SelectByTaxonomySlug", slug, culture);
        }

        public void Insert(Product entity)
        {
            SqlDataProvider.CreateInstance().ExecuteNonQuery("Proc_Product_Insert", entity);
        }

        public void Delete(Product entity)
        {
            SqlDataProvider.CreateInstance().ExecuteNonQuery("Proc_Product_Delete", entity);
        }

        public void Update(Product entity)
        {
            SqlDataProvider.CreateInstance().ExecuteNonQuery("Proc_Product_Update", entity);
        }

        #endregion
    }
}
